package cn.merryyou.generator.vo;

import cn.merryyou.generator.util.ServletUtils;
import cn.merryyou.generator.util.SqlUtil;
import cn.merryyou.generator.util.StringUtils;
import com.github.pagehelper.PageHelper;

/**
 * 表格数据处理
 *
 * @author ruoyi
 */
public class PageSupport {
    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUM = "page";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "limit";

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String IS_ASC = "isAsc";

    /**
     * 封装分页对象
     */
    public static PageDomain getPageDomain() {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(ServletUtils.getParameterToInt(PAGE_NUM));
        pageDomain.setPageSize(ServletUtils.getParameterToInt(PAGE_SIZE));
        pageDomain.setOrderByColumn(ServletUtils.getParameter(ORDER_BY_COLUMN));
        pageDomain.setIsAsc(ServletUtils.getParameter(IS_ASC));
        return pageDomain;
    }

    public static void startPage() {
        PageDomain pageDomain = PageSupport.getPageDomain();
        int pageNum = StringUtils.isNotNull(pageDomain.getPageNum()) ? pageDomain.getPageNum() : 1;
        int pageSize = StringUtils.isNotNull(pageDomain.getPageSize()) ? pageDomain.getPageSize() : 10;

        String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        PageHelper.startPage(pageNum, pageSize, orderBy);
    }

}
